<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;


class CardDelayController extends CashierBaseController{

    function index(){
        $this->display();
    }

    function doDelay(){
        $card_id=I("post.card_id");
        $delay_time=I("post.delay_time");
        $Card=M("Card");
        $d=$Card->where(['card_id'=>$card_id])->find();
        if(!$d){
            $j=[
                "status"=>"error",
                "data"=>"此卡不存在"
            ];
            echo json_encode($j);
            return;
        }
        if($d['is_lock']== -1){
            $j=[
                "status"=>"error",
                "data"=>"此卡已冻结，不可延期操作"
            ];
            echo json_encode($j);
            return;
        }
        if($d['delay_time']!=""){
            $j=[
                "status"=>"error",
                "data"=>"此卡已延期，请先取消延期再重新延期"
            ];
            echo json_encode($j);
            return;
        }

        $delay_time = explode("~" , $delay_time);
        $delay_start_time=date("Y-m-d H:i:s",strtotime($delay_time[0]));
        $delay_end_time=date("Y-m-d H:i:s",strtotime("+1 Days",strtotime($delay_time[1])));

        if(strtotime($d['create_time'])> strtotime($delay_start_time) || strtotime($d['expire_time'])< strtotime($delay_end_time)){
            $j=[
                "status"=>"error",
                "data"=>"冻结日期不在会员有效期内"
            ];
            echo json_encode($j);
            return;
        }
        $user_id=$d['user_id'];
        $c=M('View_order_has_book')->where(['user_id'=>$user_id,'type'=>1,'is_backed'=>0])->count();
        if($c>0){
            $j=[
                "status"=>"error",
                "data"=>"图书未全部归还，不可延期操作"
            ];
            echo json_encode($j);
            return;
        }
        $delay_day=(strtotime($delay_end_time)-strtotime($delay_start_time))/86400;
        $d_delay=['start_time'=>$delay_start_time,"end_time"=>$delay_end_time];
        $data=[
            "expire_time"=>date("Y-m-d H:i:s",strtotime('+'.$delay_day.' Days',strtotime($d['expire_time']))),
            'delay_time'=>json_encode($d_delay),
            'remark'=>$d['remark']."于".date("Y-m-d H:i:s")."延期操作,冻结时间段:".$delay_start_time.'~'.$delay_end_time.";<br>"
        ];
        $d=$Card->where(['card_id'=>$card_id])->data($data)->save();
        if($d){
            $j=[
                "status"=>"success",
                "data"=>"延期成功"
            ];
            echo json_encode($j);
            return;
        }else{
            $j=[
                "status"=>"error",
                "data"=>"延期操作失败，系统错误"
            ];
            echo json_encode($j);
            return;
        }
    }
    //取消延期操作
    function doUndelay(){
        $card_id=I("post.card_id");
        $Card=M('Card');
        $d=$Card->where(['card_id'=>$card_id])->find();
        if(!$d){
            $j=[
                "status"=>"error",
                "data"=>"此卡不存在"
            ];
            echo json_encode($j);
            return;
        }

        if($d['delay_time']==""){
            $j=[
                "status"=>"error",
                "data"=>"此卡未延期，无法取消"
            ];
            echo json_encode($j);
            return;
        }
        $now=time();
        $delay_time=json_decode($d['delay_time']);
        if(strtotime($delay_time->end_time) < $now){    //如果已经超过延期的结束
            $r=$Card->where(['card_id'=>$card_id])->data(['delay_time'=>'','remark'=>$d['remark']."于".date("Y-m-d H:i:s")."取消延期操作;<br>"])->save();
            if($r){
                $j=[
                    "status"=>"error",
                    "data"=>"此卡延期时间已过，成功取消"
                ];
                echo json_encode($j);
                return;
            }else{
                $j=[
                    "status"=>"error",
                    "data"=>"此卡延期时间已过，取消失败，系统错误"
                ];
                echo json_encode($j);
                return;
            }
        }
        //如果延期时间还未结束
        $delay_start_time=$delay_time->start_time;  //延期开始时间
        $delay_end_time=$delay_time->end_time;      //延期结束时间
        if($now < strtotime($delay_start_time)){    //如果延期时段还未开始
            $delay_day=(strtotime($delay_end_time)-strtotime($delay_start_time)); //延期时长（秒）
        }else{
            $delay_day=(strtotime($delay_end_time)-$now);                         //剩余延期时长（秒）
        }
        $data=[
            "expire_time"=>date("Y-m-d H:i:s",(strtotime($d['expire_time'])-$delay_day)),
            'delay_time'=>'',
            'remark'=>$d['remark']."于".date("Y-m-d H:i:s")."取消延期操作,取消冻结时间段:".$delay_start_time.'~'.$delay_end_time.";<br>".$delay_day.$d['expire_time'].'<br>'
        ];
        $d=$Card->where(['card_id'=>$card_id])->data($data)->save();
        if($d){
            $j=[
                "status"=>"success",
                "data"=>"成功取消延期"
            ];
            echo json_encode($j);
            return;
        }else{
            $j=[
                "status"=>"error",
                "data"=>"取消失败，系统错误"
            ];
            echo json_encode($j);
            return;
        }
    }
}